package com.ruoyi.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.cg.dto.QueryOrderDto;
import com.ruoyi.common.core.cg.pojo.*;
import com.ruoyi.common.core.cg.result.OrderDetailResult;
import com.ruoyi.common.core.cg.result.PageResult;
import com.ruoyi.common.core.cg.vo.OrderInfoVo;
import com.ruoyi.user.mapper.OrderMapper;
import com.ruoyi.user.service.CommunityService;
import com.ruoyi.user.service.GroupService;
import com.ruoyi.user.service.LeaderService;
import com.ruoyi.user.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper mapper;
    @Autowired
    private CommunityService communityService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private GroupService groupService;
    @Autowired
    private LeaderService leaderService;
    /**
     * 添加订单
     * @param order 订单
     */
    @Override
    public void addOrder(Order order) {
        mapper.addOrder(order);
    }

    /**
     * 修改订单付款状态
     * @param onumber
     */
    @Override
    public void updatePayment(String onumber) {
        mapper.updatePayment(onumber);
    }

    /**
     *  通过订单Id查询Order
     * @param orderId
     * @param userId
     * @return
     */
    @Override
    public OrderDetailResult findOrderById(Integer orderId,Integer userId) {
        //1.通过用户ID查询用户所处社区
        Community community = communityService.findByUserId(userId);
        //2.通过订单ID查询所属店铺
        Store store = orderService.findStoreByOrderId(orderId);
        //3.通过订单ID查询对应订单
        Order order = orderService.findById(orderId);
        //4.通过订单ID查询对应的团
        Group group = orderService.findGroupByOrderId(orderId);
        //5.通过店铺ID查询对应的团长
        Leader leader = leaderService.findLeaderByStoreId(store.getId());
        //6.查询用户所属的是第几个团
        Integer people = groupMember(group.getPeople());
        Integer groupIndex = 1;
        // 查询该团所有的成员
        List<User> groupMemberList = groupService.findGroupAllMemberByGroupId(group.getId());
        for (int i = 0; i < groupMemberList.size();) {
            if (userId == groupMemberList.get(i).getUserId()){
                break;
            }
            if (i != 0 && i % 3 == 0){
                groupIndex ++;
            }
            i++;
        }
        // 查询用户所处团的所有用户
        Integer startIndex = (groupIndex - 1)*people;
        Integer endIndex = (people*groupIndex);
        List<User> groupMember = groupService.findGroupMemberByGroupId(group.getId(),startIndex,endIndex);
        //7.封装返回结果
        OrderDetailResult orderDetailResult = new OrderDetailResult();
        orderDetailResult.setOrder(order);
        orderDetailResult.setGroup(group);
        orderDetailResult.setLeader(leader);
        orderDetailResult.setStore(store);
        orderDetailResult.setCommunity(community);
        orderDetailResult.setUserList(groupMember);
        orderDetailResult.setBelongToGroup(groupIndex);
        return orderDetailResult;
    }

    /**
     * 通过订单ID查询店铺
     * @param orderId
     * @return
     */
    @Override
    public Store findStoreByOrderId(Integer orderId) {
        return mapper.findStoreByOrderId(orderId);
    }

    /**
     * 通过订单ID查询订单
     * @param orderId
     * @return
     */
    @Override
    public Order findById(Integer orderId) {
        return mapper.findById(orderId);
    }

    /**
     * 通过订单ID查询所属团
     * @param orderId
     * @return
     */
    @Override
    public Group findGroupByOrderId(Integer orderId) {
        return mapper.findGroupByOrderId(orderId);
    }

    /**
     * 通过订单状态查询用户
     * @param queryOrderDto
     * @return
     */
    @Override
    public PageResult findUserOrderByState(QueryOrderDto queryOrderDto) {
        PageHelper.startPage(queryOrderDto.getCurrentPage(),queryOrderDto.getPageSize());
        // 查询订单
        List<OrderInfoVo> orderList = mapper.findUserOrderByState(queryOrderDto);

        PageInfo<Order> pageInfo = new PageInfo<>(orderList);
        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 查询属性对应的人数
     * @param type
     * @return
     */
    private Integer groupMember(Integer type){
        if (type == 0){
            return 3;
        }else if (type == 1){
            return 5;
        }else if (type == 2){
            return 10;
        }else if (type == 3){
            return 20;
        }
        return 0;
    }
}
